package pt.compflow.activity.gateway;

import pt.compflow.flowontology.model.Activity;
import pt.compflow.flowontology.model.FLOWO;
import pt.compflow.flowontology.model.FlowOntologyModel;
import pt.compflow.flowontology.model.Gateway;
import pt.compflow.tools.OntModelHelper;

import com.hp.hpl.jena.util.iterator.ExtendedIterator;

/**
 * Gateway class that starts the gateway when any previous activity finishes.
 * 
 * @author carlos
 * 
 */
public abstract class MergeInputGateway extends GatewayExecutor {

	public MergeInputGateway() {
	}

	public MergeInputGateway(OntModelHelper ontModelHelper, FlowOntologyModel jobModel, Gateway gateway) {
		super(ontModelHelper, jobModel, gateway);
	}

	/**
	 * Checks IN conditions of previous activities. In this case, it checks if
	 * all previous activities have ended.
	 */
	public final boolean isReady() {

		ExtendedIterator<Activity> actList = gateway.listTransitionFromActivitys();
		while (actList.hasNext()) {

			Activity act = (Activity) actList.next();

			if (act == null) {
				throw new NullPointerException("Activity does not exist!");
			}

			// if any is finished, then isRunnable (it is ready to run).
			if (act.getPropertyResourceValue(FLOWO.hasState).getURI().compareTo(FLOWO.finished.getURI()) == 0) {
				return true;
			}

		}

		return false;

	}

}
